package com.bjtg.threegoclinet.util;

import android.media.MediaPlayer;
import android.util.Log;

import com.bjtg.threegoclinet.base.MyApplication;

import java.io.IOException;

/**
 * Created by qilee on 2017/11/14.
 */

public class MediaPlayControl {
    private MediaPlayer mediaPlayer;

    private static final class MediaPlayControlHolder {
        private static final MediaPlayControl INSTANCE = new MediaPlayControl();
    }

    public static MediaPlayControl getInstance() {
        return MediaPlayControlHolder.INSTANCE;
    }

    public void play(int resid) {
        play(resid, null);
    }

    public void play(int resid, MediaPlayer.OnCompletionListener completionListener) {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
                mediaPlayer = MediaPlayer.create(MyApplication.getApplicationMyContext(), resid);
                mediaPlayer.setOnCompletionListener(completionListener);
                mediaPlayer.start();
            } else {
                mediaPlayer = MediaPlayer.create(MyApplication.getApplicationMyContext(), resid);
                mediaPlayer.setOnPreparedListener(mp -> Log.e("tzy", "语音准备完毕"));
                mediaPlayer.setOnCompletionListener(completionListener);
                mediaPlayer.start();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void play(String voicePath) {
        play(voicePath, null);
    }

    public void play(String voicePath, MediaPlayer.OnCompletionListener completionListener) {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
                mediaPlayer = new MediaPlayer();
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(voicePath);
                    mediaPlayer.setOnCompletionListener(completionListener);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {

                mediaPlayer = new MediaPlayer();
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(voicePath);

                    mediaPlayer.setOnPreparedListener(mp -> Log.e("tzy", "语音准备完毕"));
                    mediaPlayer.setOnCompletionListener(completionListener);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void release() {

    }

    public void stopPlay() {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }
    public boolean isPlaying(){
        if (mediaPlayer != null){
            return mediaPlayer.isPlaying();
        }
       return false;
    }
    public void reStart() {
        if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }
}
